package com.ysstech.finance.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysstech.common.cache.RedisCacheManager;
import com.ysstech.common.entity.TreeNode;
import com.ysstech.finance.entity.Account;
import com.ysstech.finance.mapper.FinAccountMapper;
import com.ysstech.finance.service.FinAccountService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lishuangliang
 * @since 2021-02-18
 */
@Service
public class FinAccountServiceImpl extends ServiceImpl<FinAccountMapper, Account> implements FinAccountService {
    @Autowired
    private RedisCacheManager redisCacheManager;

    /**
     * 查询套账树信息
     *
     * @return
     */
    public List<TreeNode> queryAccountTree(Account account) throws Exception {
        List<TreeNode> list = new ArrayList<>();
        QueryWrapper<Account> wrapper = new QueryWrapper<>();
        if (null != account) {
            if (null != account.getType()) {
                wrapper.eq("TYPE", account.getType());
            }
            if (null != account.getFundType()) {
                wrapper.eq("FUND_TYPE", account.getFundType());
            }
            if (null != account.getFundClass()) {
                wrapper.eq("FUND_CLASS", account.getFundClass());
            }
        }
        if (StringUtils.isNotBlank(account.getOrgId())) {
            wrapper.eq("ORG_ID", account.getOrgId());
        } else {
            wrapper.eq("ORG_ID", redisCacheManager.getLoginUserInfo().getOrgId());
        }
        if (null != account.getFundType()) {
            wrapper.eq("FUND_TYPE", account.getFundType());
        }
        if (null != account.getType()) {
            wrapper.eq("TYPE", account.getType());
        }
        wrapper.eq("STATUS", 1);
        wrapper.orderByDesc("ADD_TIME");
        List<Account> listaccount = this.list(wrapper);
        if (CollectionUtils.isEmpty(listaccount)) {
            return list;
        }
        list.addAll(this.recursionTree("0", listaccount));
        return list;
    }


    private List<TreeNode> recursionTree(String orgId, List<Account> listaccont) throws Exception {
        List<TreeNode> list = new ArrayList<>();
        for (Account account : listaccont) {
            if (!orgId.equals(account.getParentId())) {
                continue;
            }
            TreeNode treeNode = organizationToNode(account);
            List<TreeNode> children = recursionTree(account.getId(), listaccont);
            if (null == children || children.size() == 0) {
                treeNode.setState("open");
            } else {
                treeNode.setChildren(children);
            }
            list.add(treeNode);
        }
        return list;
    }

    /**
     * 将业务功能转化成TreeNode
     */
    private TreeNode organizationToNode(Account account) {
        TreeNode node = new TreeNode();
        node.setId(account.getId());
        node.setText(account.getName());
        node.setState("closed");
        node.setChecked(false);
        node.setIconCls("icon-bricks");
        node.setPId(account.getParentId());
        node.setAttributes(account.getCode());
        return node;
    }
}
